package com.addTools;

import java.sql.Time;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;

public class CyclicBarrierTest {
    public static void main(String[] args) throws BrokenBarrierException, InterruptedException {
        //集齐7颗龙珠就可以召唤神龙
        CyclicBarrier cyclicBarrier = new CyclicBarrier(7,()->{
            System.out.println("召唤神龙");

        });
        for (int i = 1; i <= 7; i++) {
            //lambda操作不到i，需要一个final来实现变量转换
            final int temp = i;
            new Thread(()->{
                System.out.println(temp+"召唤龙珠"+Thread.currentThread().getName()+"号");
                System.out.println(cyclicBarrier.getNumberWaiting());
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            },String.valueOf(i)).start();
        }
        TimeUnit.SECONDS.sleep(2);
        System.out.println(cyclicBarrier.getParties());

    }


}


